#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (int i=t;i>=h;i--)
const int N=1.3e5;
struct re{
	int a;
	ll b,c;
	bool operator <(const re x) const {
		if (a!=x.a) return a<x.a;
		if (b!=x.b) return b<x.b;
		return c<x.c;
	}
};
vector<re> ve1[N],ve2[N];
struct re2{
	int a,b;
	char c; int d;
};
vector<re2> ve[N],ve3[N];
ll ans2,ans3,ans;
int a[N],b[N],o[N],n,m; 
int cntt=0;
void solve(int h1,int t1,int h2,int t2,int k,int x,int y,int sum)
{
	if (x>=h1&&x<=t1&&y>=h2&&y<=t2) ans-=k;;
	if (h1>t1||h2>t2) return;
	h1=max(h1,1); t1=min(2*m,t1); h2=max(h2,1); t2=min(2*m,t2); 
	cntt++; 
	ve[h1-1].push_back({h2,t2,-k,sum});
	ve[t1].push_back({h2,t2,k,sum});
}
map<pair<int,int>,int> M,M2;
void cl(int i,int k,ll sum)
{
	  re t1={b[i]-k,0,0},t2={b[i]+k,(int)(1e9),0};
	  int pp=0;
	  if (a[i]-k>0)
	  {
	    auto i1=upper_bound(ve1[a[i]-k].begin(),ve1[a[i]-k].end(),t2),i2=lower_bound(ve1[a[i]-k].begin(),ve1[a[i]-k].end(),t1);
	    pp+=i1-i2;
	    ans2+=(*i1).b-(*i2).b;
	  }
	  if (a[i]+k<=2*m)
	  {
	    auto i1=upper_bound(ve1[a[i]+k].begin(),ve1[a[i]+k].end(),t2),i2=lower_bound(ve1[a[i]+k].begin(),ve1[a[i]+k].end(),t1);
	    pp+=i1-i2;
	    ans2+=(*i1).b-(*i2).b;
	  }
	  t1.a=a[i]-k+1,t2.a=a[i]+k-1;
	  if (b[i]-k>0)
	  {
	     auto i1= upper_bound(ve2[b[i]-k].begin(),ve2[b[i]-k].end(),t2),i2=lower_bound(ve2[b[i]-k].begin(),ve2[b[i]-k].end(),t1);
		 pp+=i1-i2;
		 ans2+=(*i1).b-(*i2).b;
	  } 
	  if (b[i]+k<=2*m)
	  {
	    auto i1=upper_bound(ve2[b[i]+k].begin(),ve2[b[i]+k].end(),t2),i2=lower_bound(ve2[b[i]+k].begin(),ve2[b[i]+k].end(),t1);
	    pp+=i1-i2;
	    ans2+=(*i1).b-(*i2).b;
	  }
	  ans2+=pp*sum;
}
#define lowbit(x) (x&(-x))
struct sgt{
	ll v[N];
	void g(int x,int y)
	{
		while (x<=2*m)
		{
			v[x]+=y;
			x+=lowbit(x);
	    }
	}
	ll q2(int x)
	{
		ll ans=0;
		while (x>0)
		{
			ans+=v[x];
			x-=lowbit(x);
		}
		return ans;
	}
	ll q(int x,int y)
	{
		return q2(y)-q2(x-1);
	}
}S1,S2;
const int MN=3e5;
int pri[MN+10],cntpri;
bool vis[MN+10];
int main()
{ 
   freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout); 
    ios::sync_with_stdio(false);
	vis[1]=1;
	rep(i,2,MN){
        if(!vis[i]) { pri[++cntpri]=i;}
        for(int j=1;j<=cntpri&&i*pri[j]<=MN;++j){
            vis[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
	
	rep(i,1,MN) if (i%20!=3&&i>10) vis[i]=1;
	int T;
	cin>>T;
	while(T--)
	{
	    ans=0,ans2=0,ans3=0;
		cin>>n>>m;
		rep(i,1,n) cin>>a[i]>>b[i];
		rep(i,1,n) cin>>o[i];
		M.clear(); M2.clear(); 
		rep(i,1,n)
		{
			int t1=a[i]-b[i]+m,t2=a[i]+b[i];
			a[i]=t1,b[i]=t2;
			M[{t1,t2}]++; M2[{t1,t2}]+=o[i]; 
			ve1[t1].push_back({t2,o[i]});
			ve2[t2].push_back({t1,o[i]});
		}
		rep(i,1,2*m) sort(ve1[i].begin(),ve1[i].end()),sort(ve2[i].begin(),ve2[i].end());
		rep(i,1,2*m)
		{
			ll sum=0;
			for (auto &v:ve1[i]) {v.c=v.b;v.b=sum; sum+=v.c;}
			ve1[i].push_back({(int)(1e9),sum});
			sum=0;
			for (auto &v:ve2[i]) {v.c=v.b;v.b=sum; sum+=v.c;} 
			ve2[i].push_back({(int)(1e9),sum});
		}		
		rep(i,1,n)
		{
			rep(j,1,n)
			if (i!=j) 
			{
				int k=max(abs(a[i]-a[j]),abs(b[i]-b[j]));
				if (vis[k]) continue;
				cl(i,k,o[i]+o[j]); ans2-=o[i]+o[j]+o[j];
				solve(a[i]-(k-1),a[i]+(k-1),b[i]-(k-1),b[i]+(k-1),1,a[i],b[i],o[i]+o[j]);
				solve(max(a[i]-(k-1),a[j]-k),min(a[i]+(k-1),a[j]+k),max(b[i]-(k-1),b[j]-k),min(b[i]+(k-1),b[j]+k),-1,a[i],b[i],o[i]+o[j]);
				if (i>j) continue;
				int pp=0; 
				if (a[i]==a[j])
				{
					re t1={min(b[i],b[j]),0,0},t2={max(b[i],b[j]),(int)(1e9),0};
					if (a[i]-k>0)
					{
					  auto i1=upper_bound(ve1[a[i]-k].begin(),ve1[a[i]-k].end(),t2),i2=lower_bound(ve1[a[i]-k].begin(),ve1[a[i]-k].end(),t1);
					  pp+=i1-i2; ans3+=(*i1).b-(*i2).b;
				    }
				    if (a[i]+k<=2*m)
				    {
                      auto i1=upper_bound(ve1[a[i]+k].begin(),ve1[a[i]+k].end(),t2),i2=lower_bound(ve1[a[i]+k].begin(),ve1[a[i]+k].end(),t1);
                      pp+=i1-i2; ans3+=(*i1).b-(*i2).b;
                    }
				} else
				if (b[i]==b[j])
				{
					re t1={min(a[i],a[j]),0,0},t2={max(a[i],a[j]),(int)(1e9),0};
					if (b[i]-k>0)
					{
					  auto i1=upper_bound(ve2[b[i]-k].begin(),ve2[b[i]-k].end(),t2),i2=lower_bound(ve2[b[i]-k].begin(),ve2[b[i]-k].end(),t1);
					  pp+=i1-i2; ans3+=(*i1).b-(*i2).b;
				    }
				    if (b[i]+k<=2*m)
				    {
				      auto i1=upper_bound(ve2[b[i]+k].begin(),ve2[b[i]+k].end(),t2),i2=lower_bound(ve2[b[i]+k].begin(),ve2[b[i]+k].end(),t1);
					  pp+=i1-i2; ans3+=(*i1).b-(*i2).b;
				    }
				} else
				{
					int v1[2],v2[2];
					if (a[j]<a[i]) v1[0]=a[i]-k,v1[1]=a[j]+k;
					else v1[0]=a[i]+k,v1[1]=a[j]-k;
					if (b[j]<b[i]) v2[0]=b[i]-k,v2[1]=b[j]+k;
					else v2[0]=b[i]+k,v2[1]=b[j]-k;
					if (M.find({v1[0],v2[1]})!=M.end()) pp+=M[{v1[0],v2[1]}];
					if (M.find({v1[1],v2[0]})!=M.end()) pp+=M[{v1[1],v2[0]}];
					if (M2.find({v1[0],v2[1]})!=M2.end()) ans3+=M2[{v1[0],v2[1]}];
					if (M2.find({v1[1],v2[0]})!=M2.end()) ans3+=M2[{v1[1],v2[0]}];
				}
				ans3+=1ll*pp*(o[i]+o[j]);
			}
		}
		rep(i,1,2*m)
		{
			for (auto v:ve1[i]) 
			  if (v.a!=1e9) S1.g(v.a,1);
			for (auto v:ve[i]) 
			  ans+=1ll*S1.q(v.a,v.b)*v.c*v.d;
			for (auto v:ve[i-1])
			{ 
			  S2.g(v.a,-v.c),S2.g(v.b+1,v.c);
		    }
			for (auto v:ve1[i]) 
			  if (v.a!=1e9) ans+=1ll*S2.q2(v.a)*v.c;
	    }
		cout<<ans+ans2/2-ans3/3*2<<endl;
		rep(i,0,4*m) ve[i].clear(),ve1[i].clear(),ve2[i].clear(),ve3[i].clear(),S1.v[i]=S2.v[i]=0;
	}
	return 0;
}
/* 
2
3 3
1 1
2 2
3 3
3 3
1 1
2 1
3 2
*/
